<?php

namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\facade\Filesystem;

class Login extends BaseController
{
    // POST $phoneNumber $password
    public function login()
    {
        $result = [
            "status" => false,
            "description" => "未知错误",
            "userId" => ""
        ];

        $login_phoneNumber = Request::post("phoneNumber");
        $login_password = Request::post("password");
        
        if($login_phoneNumber != null && $login_password != null) {
            $find_result = Db::table("ZZZ_login_info")->where("phoneNumber", $login_phoneNumber)->find();
            $hash_password = hash("sha256", $login_password, false);

            if($find_result != null && $hash_password == $find_result["password"]){
                $result["status"] = true;
                $result["descrption"] = "success";
                
                $result["userId"] = $find_result["userId"];
            } else {
                $result["description"] = "手机号或者密码错误";
            }
        } else {
            $result["description"] = "输入的手机号或者密码为空";
        }

        return json($result);
    }

    // POST $phoneNumber $password
    public function register()
    {
        $result = [
            "status" => false,
            "description" => "未知错误"
        ];

        $register_phoneNumber = Request::post("phoneNumber");
        $register_password = Request::post("password");

        if($register_phoneNumber != null && $register_password != null) {
            $find_result = Db::table("ZZZ_login_info")->where("phoneNumber", $register_phoneNumber)->find();

            if($find_result == null){
                $hash_password = hash("sha256", $register_password, false);
                $unique_userId = "U" . uniqid();
                
                $save_login_result = Db::table("ZZZ_login_info")->insert([
                    "phoneNumber" => $register_phoneNumber,
                    "password" => $hash_password,
                    "userId" => $unique_userId
                ]);

                $save_user_result = Db::table("ZZZ_user_info")->insert([
                    "userId" => $unique_userId
                ]);

                if($save_login_result == 1 && $save_user_result == 1) {
                    $result["status"] = true;
                    $result["description"] = "success";
                }
            } else {
                $result["description"] = "该手机号已经被注册";
            }
        } else {
            $result["description"] = "输入的手机号或者密码为空";
        }

        return json($result);
    } 

    // GET $userId
    public function userinfo()
    {
        $result = [
            "status" => false,
            "description" => "未知错误",
            "nickName" => "转转转",
            "avatarAddress" => "http://62.234.45.120:1230/static/avatar/default.png",
            "grade" => 0,
            "campus" => "",
            "college" => "",
            "contact" => "",
            "realNameStage" => false,
            "realStudentNumber" => "",
            "realName" => ""
        ];

        $userId = Request::get("userId");

        if($userId != null) {
            $find_result = Db::table("ZZZ_user_info")->where("userId", $userId)->find();

            if($find_result != null) {
                $result["status"] = true;
                $result["description"] = "success";

                $result["nickName"] = $find_result["nickName"];
                $result["avatarAddress"] = $find_result["avatarAddress"];
                $result["grade"] = $find_result["grade"] == null ? 0 : $find_result["grade"];
                $result["campus"] = $find_result["campus"] == null ? "" : $find_result["campus"];
                $result["college"] = $find_result["college"] == null ? "" : $find_result["college"];
                $result["contact"] = $find_result["contact"] == null ? "" : $find_result["contact"];
                $result["realNameStage"] = $find_result["realNameStage"];
                $result["realStudentNumber"] = $find_result["realStudentNumber"] == null ? "" : $find_result["realStudentNumber"];
                $result["realName"] = $find_result["realName"] == null ? "" : $find_result["realName"];
            } else {
                $result["description"] = "请求出错";
            }
        } else {
            $result["description"] = "请求出错";
        }

        return json($result);
    }

    // POST $userId + Others
    public function modifyuserinfo()
    {
        $result = [
            "status" => false,
            "description" => "未知错误"
        ];

        $userId = Request::post("userId");

        $newNickName = Request::post("nickName");
        $newGrade = Request::post("grade");
        $newCampus = Request::post("campus");
        $newCollege = Request::post("college");

        $newRealNameStage = Request::post("realNameStage");
        $newRealStudentNumber = Request::post("realStudentNumber");
        $newRealName = Request::post("realName");

        if($userId != null){
            $result["status"] = true;
            $result["description"] = "success";
            
            if($newNickName != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["nickName" => $newNickName]);
            }

            if($newGrade != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["grade" => $newGrade]);
            }

            if($newCampus != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["campus" => $newCampus]);
            }

            if($newCollege != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["college" => $newCollege]);
            }


            if($newRealNameStage != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["realNameStage" => $newRealNameStage]);
            }

            if($newRealStudentNumber != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["realStudentNumber" => $newRealStudentNumber]);
            }

            if($newRealName != null) {
                Db::table("ZZZ_user_info")
                ->where("userId", $userId)
                ->update(["realName" => $newRealName]);
            }
        } else {
            $result["description"] = "请求出错";
        }
        return json($result);
    }

    // POST
    // GET $bookId
    // FILE(POST) $bookImage
    public function uploadavatar()
    {
        $result = [
            "status" => false,
            "description" => "未知错误",
        ];

        $userId = Request::get("userId");
        $file = Request::file('avatarImage');
        
        if($userId != null && $file != null) {
            $unique_avatarName = "A" . uniqid();

            $imageLocalAddress = Filesystem::disk("public")->putFileAs("avatar", $file, $unique_avatarName . ".jpeg");

            $imageRemoteAddress = "http://62.234.45.120:1230/storage/" . $imageLocalAddress;

            $db_update_result = Db::table("ZZZ_user_info")
            ->where("userId", $userId)
            ->update(["avatarAddress" => $imageRemoteAddress]);

            if($db_update_result == 1) {
                $result["status"] = true;
                $result["description"] = "success";
            }
        } else {
            $result["description"] = $userId;
        }
        
        return json($result);
    }
}
